package com.example.springboot.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.AuthAccess;
import com.example.springboot.common.CcLogs;
import com.example.springboot.common.LogType;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Orders;
import com.example.springboot.service.OrdersService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单表前端操作接口
 **/
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Resource
    private OrdersService ordersService;

    /**
     * 新增
     */
    @AuthAccess
    @PostMapping("/add")
    @CcLogs(operation = "订单信息",type = LogType.ADD)
    public Result add(@RequestBody Orders orders) {
        try {
            ordersService.add(orders);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @CcLogs(operation = "订单信息",type = LogType.DELETE)
    public Result deleteById(@PathVariable Integer id) {
        ordersService.removeById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/double")
    @CcLogs(operation = "订单信息",type = LogType.DELETE)
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        ordersService.removeBatchByIds(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @CcLogs(operation = "订单信息",type = LogType.UPDATE)
    public Result updateById(@RequestBody Orders orders) {
        ordersService.updateById(orders);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Orders orders = ordersService.getById(id);
        return Result.success(orders);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(Orders orders) {
        List<Orders> ordersList = ordersService.list(new QueryWrapper<Orders>().orderByDesc("id"));
        return Result.success(ordersList);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectByPage")
    public Result selectPage(@RequestParam(required = false) String goodsName,
                             @RequestParam Integer pageNum,
                             @RequestParam Integer pageSize) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<Orders>().orderByDesc("id");
        queryWrapper.like(StrUtil.isNotBlank(goodsName), "goods_name", goodsName);
        Page<Orders> page = ordersService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

    /**
     * 分页查询2
     */
    @GetMapping("/selectByPage2")
    public Result selectPage2(@RequestParam(required = false) String goodsName,
                             @RequestParam Integer pageNum,
                             @RequestParam Integer pageSize,
                             @RequestParam(required = false) Integer useId) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<Orders>().orderByDesc("id");
        if (useId != null) {
            queryWrapper.eq("use_id", useId); // 假设Orders表中有一个user_id字段来存储用户ID
        }
        queryWrapper.like(StrUtil.isNotBlank(goodsName), "goods_name", goodsName);
        Page<Orders> page = ordersService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

}